import * as types from '../_types/market';
import { IReduxMarketItem } from '../interface';

interface IState{
    market: Array<IReduxMarketItem>,
    amountToCNY: number,
}

const initialState = {
    market: [],
    amountToCNY: 6.8
}

export default function updateMarket(state:IState = initialState, action) {
    const {type, payload} = action;
    switch (type) {
        case types.UPDATE_MAKERT_DATA: {
            return Object.assign({}, state, {market: payload});
        }
        case types.UPDATE_AMOUNTTO: {
            return Object.assign({}, state, {amountToCNY: payload});
        }
        case types.UPDATE_ONE_MARKET_ITEM: {
            let {market} = state;
            let index = market.findIndex(v => v.type === payload.type);
            if (index < 0) {
                market.push(payload);
            } else {
                market[index] = payload;
            }
            Object.assign({}, market, payload) ;
            return Object.assign({}, state, {market});
        }
        default: {
            return state;
        }
    }
}